package com.util;

import com.controller.WebViewController;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.awt.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Ricky
 * @date 2020-8-12 16:46
 */
public class ControllerManager {
    public static Map<String, WebViewController> controllerMap = new HashMap<>();
    public static Map<String, Stage> stageMap = new HashMap<>();

    public static void init(Stage primaryStage) throws Exception {

        WebViewController primaryController = new WebViewController("primary");
        WebViewController secondaryController = new WebViewController("secondary");

        controllerMap.put(primaryController.getFxmlName(), primaryController);
        controllerMap.put(secondaryController.getFxmlName(), secondaryController);

        createPrimaryStage(primaryStage);
        createSecondaryStage();
    }

    private static void createPrimaryStage(Stage primaryStage) throws Exception {
        primaryStage.setTitle("出庭一体化平台");
        Scene scene = getSceneByFxmlName("primary");
        primaryStage.initStyle(StageStyle.UNDECORATED);
        primaryStage.setScene(scene);
        primaryStage.show();
        stageMap.put("primary", primaryStage);
    }

    private static void createSecondaryStage() throws Exception {
        Stage secondaryStage = new Stage();
        secondaryStage.setTitle("投屏窗口");
        Scene scene = getSceneByFxmlName("secondary");
        secondaryStage.setScene(scene);
        // 去除窗口边框
        secondaryStage.initStyle(StageStyle.UNDECORATED);
        secondaryStage.setAlwaysOnTop(true);
//        secondaryStage.setFullScreen(true);

        ScreenUtil.Screen screen = ScreenUtil.screenList.get(1);
        Rectangle fbl = screen.getFbl();
        secondaryStage.setX(fbl.x);
        secondaryStage.setY(fbl.y);
        secondaryStage.setWidth(fbl.width);
        secondaryStage.setHeight(fbl.height);
        stageMap.put("secondary", secondaryStage);
    }

    private static Scene getSceneByFxmlName(String fxmlName) throws IOException {
        FXMLLoader loader = new FXMLLoader(FxmlUtil.getURLByFxmlName("webview"));
        WebViewController controller = controllerMap.get(fxmlName);
        loader.setController(controller);
        Parent root = loader.load();
        return new Scene(root);
    }

    public static WebViewController getControllerByFxmlName(String fxmlName) {
        return controllerMap.get(fxmlName);
    }
}
